import redis


r = redis.Redis(host='127.0.0.1', port=6379, db=0)

r.hset("user001", mapping={"username": "yu",
                           "fans": 2, "focus": 10})
result = r.hget("user001", "username")
print(result.decode())

result = r.hkeys("user001")

# 列表推导式
print([i.decode() for i in result])

# {b'username': b'yu', b'fans': b'2', b'focus': b'10'}
result = r.hgetall("user001")
print(result)

# dic = {}
# for k, v in result.items():
#     dic[k.decode()] = v.decode()
# print(dic)

# print(result.items())
# dict_items([(b'username', b'yu'), (b'fans', b'2'), (b'focus', b'10')])

# 字典推导式： {key: value for var in iterable}
print({k.decode(): v.decode() for k, v in result.items()})
